package com.mycontract.suitepro.network

import androidx.lifecycle.ViewModel
import com.mycontract.suitepro.data.*
import com.mycontract.suitepro.utils.AppConfigUtil
import kotlinx.coroutines.Dispatchers

object ApiUserReposity : ApiBaseReposity() {

    //////////////////////////////
    fun getContractOrderList(
        caller: ViewModel?,
        reqGetContractList: ReqGetContractList
    ) =
        fire(Dispatchers.IO, caller, reqGetContractList.showLoading) {
            val response =
                ApiUserNetwork.getContractOrderList(reqGetContractList)
            if (response.isSuccess()) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun signContract(
        caller: ViewModel?,
        reqSignContract: ReqSignContract
    ) =
        fire(Dispatchers.IO, caller, reqSignContract.showLoading) {
            val response =
                ApiUserNetwork.signContract(reqSignContract)
            if (response.isSuccess()) {
                response.orderId = reqSignContract.orderId
                response.token = reqSignContract.token
                response.product = reqSignContract.product
                Result.success(response)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    fun getAppConfig(
        caller: ViewModel?,
        reqBase: ReqBase
    ) =
        fire(Dispatchers.IO, caller, reqBase.showLoading) {
            val response =
                ApiUserNetwork.getAppConfig(reqBase)
            if (response.isSuccess()) {
                val obj = response.data
                AppConfigUtil.mRevObjAppConfig = obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }
}